package com.learn.mvp;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.learn.mvp.presenter.ILoginPresenter;
import com.learn.mvp.presenter.LoginPresenter;
import com.learn.mvp.view.ILoginView;

public class MvpActivity extends AppCompatActivity implements ILoginView {

    private LinearLayout loginLayout;
    private ProgressBar progressBar;

    private EditText userName;
    private EditText userPassword;
    private Button loginBtn;

    private ILoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        loginLayout = (LinearLayout) findViewById(R.id.login_layout);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        userName = (EditText)findViewById(R.id.user_name);
        userPassword = (EditText)findViewById(R.id.user_password);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginPresenter = new LoginPresenter(this);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //传递账号、密码
                loginPresenter.loginToServer(userName.getText().toString(), userPassword.getText().toString());
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public void showProgress(boolean enable) {
        if (enable){
            progressBar.setVisibility(View.VISIBLE);
            loginLayout.setVisibility(View.GONE);
        }else {
            progressBar.setVisibility(View.GONE);
            loginLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoginSucceedView() {
        Toast.makeText(MvpActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoginFailedView() {
        Toast.makeText(MvpActivity.this,"账号或者密码错误",Toast.LENGTH_SHORT).show();
    }
}
